package com.koron.housing.info.service.impl;


import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.housing.admin.service.HousingAdminService;
import com.koron.housing.info.bean.po.HousingRecord;
import com.koron.housing.info.bean.query.HousingRecordQuery;
import com.koron.housing.info.mapper.HousingRecordMapper;
import com.koron.housing.info.service.HousingRecordService;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 租房记录(HousingRecord)表服务实现类
 *
 * @author jzp
 * @since 2022-02-25 17:18:06
 */
@Service("recordService")
public class HousingRecordServiceImpl implements HousingRecordService {

    @Autowired
    private HousingAdminService adminService;

    /**
     * 通过ID查询单条数据
     */
    @Override
    @TaskAnnotation("getById")
    public HousingRecord getById(SessionFactory factory, String id) {
        HousingRecordMapper mapper = factory.getMapper(HousingRecordMapper.class);
        return mapper.selectById(id);
    }

    @Override
    @TaskAnnotation("getNewByHousingIdAndAccount")
    public HousingRecord getNewByHousingIdAndAccount(SessionFactory factory, String infoId, String account) {
        HousingRecordMapper mapper = factory.getMapper(HousingRecordMapper.class);
        return mapper.selectNewByHousingIdAndAccount(infoId, account);
    }

    /**
     * 查询所有数据
     */
    @Override
    @TaskAnnotation("list")
    public List<HousingRecord> list(SessionFactory factory, HousingRecordQuery query) {
        HousingRecordMapper mapper = factory.getMapper(HousingRecordMapper.class);
        return mapper.selectList(query);
    }

    @Override
    @TaskAnnotation("listByHousingId")
    public List<HousingRecord> listByHousingId(SessionFactory factory, String housingId) {
        HousingRecordQuery housingRecordQuery = new HousingRecordQuery();
        housingRecordQuery.setHousingId(housingId);
        return list(factory, housingRecordQuery);
    }

    @Override
    @TaskAnnotation("page")
    public PageInfo<HousingRecord> page(SessionFactory factory, PageQuery pageQuery, HousingRecordQuery query) {
        boolean isAdmin = adminService.isAdminOfCurr(factory);
        pageQuery.prefixOrders("housingRecord", null);
        return isAdmin ? HousingRecordService.super.page(factory, pageQuery, query) : null;
    }

    /**
     * 新增数据
     */
    @Override
    @TaskAnnotation("save")
    public boolean save(SessionFactory factory, HousingRecord housingRecord) {
        HousingRecordMapper mapper = factory.getMapper(HousingRecordMapper.class);
        return mapper.insertOrUpdate(housingRecord) > 0;
    }

    @Override
    @TaskAnnotation("saveOrUpdate")
    public boolean saveOrUpdate(SessionFactory factory, HousingRecord housingRecord) {
        return StringUtils.isEmpty(housingRecord.getId()) ? save(factory, housingRecord) : update(factory, housingRecord);
    }

    /**
     * 修改数据
     */
    @Override
    @TaskAnnotation("update")
    public boolean update(SessionFactory factory, HousingRecord housingRecord) {
        HousingRecordMapper mapper = factory.getMapper(HousingRecordMapper.class);
        return mapper.update(housingRecord) > 0;
    }

    /**
     * 通过主键删除数据
     */
    @Override
    @TaskAnnotation("deleteById")
    public boolean deleteById(SessionFactory factory, String id) {
        HousingRecordMapper mapper = factory.getMapper(HousingRecordMapper.class);
        return mapper.hidden(id) > 0;
    }

    @Override
    @TaskAnnotation("saveNew")
    public boolean saveNew(SessionFactory factory, HousingRecord housingRecord) {
        HousingRecord source = getNewByHousingIdAndAccount(factory, housingRecord.getHousingId(), housingRecord.getAccount());
        if (source != null && source.getOutDate() == null) {
            housingRecord.setId(source.getId());
        }
        return saveOrUpdate(factory, housingRecord);
    }
}
